package com.ofek.eventos;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.ofek.applicationbase.broadcast.Broadcaster;
import com.ofek.applicationbase.broadcast.Delegate;

public class ToolController {
	private final ViewGroup tool;

	public interface ToolListener {
		public void onEditChecked(boolean b);
		public void onAddClicked();
		public void onSearchClicked();
		public void onSyncClicked();
	}
	private final Broadcaster<ToolListener> editBroadcaster = new Broadcaster<ToolListener>();
	final TextView header;
	final TextView subHeader;
	final CompoundButton edit;


	public ToolController(final ViewGroup tool) {
		this.tool = tool;
		header = (TextView)tool.findViewById(R.id.header_text);
		subHeader = (TextView)tool.findViewById(R.id.sub_header_text);
		edit = (ToggleButton)tool.findViewById(R.id.edit);
		edit.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
				editBroadcaster.broadcast(new Delegate<ToolListener>() {
					@Override
					public void invoke(final ToolListener t) {
						t.onEditChecked(isChecked);
						final Intent intent = new Intent(Constants.EDIT_REQUEST);
						intent.putExtra(Constants.EXTRA_DATA, isChecked);
						LocalBroadcastManager.getInstance(tool.getContext()).sendBroadcast(intent);
					}
				});
			}
		});

		final ImageButton add = (ImageButton)tool.findViewById(R.id.add);

		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				editBroadcaster.broadcast(new Delegate<ToolListener>() {
					@Override
					public void invoke(final ToolListener t) {
						t.onAddClicked();
						final Intent intent = new Intent(Constants.ADD_REQUEST);
						LocalBroadcastManager.getInstance(tool.getContext()).sendBroadcast(intent);
					}
				});
			}
		});
		final ImageButton sync = (ImageButton)tool.findViewById(R.id.sync);

		sync.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				editBroadcaster.broadcast(new Delegate<ToolListener>() {
					@Override
					public void invoke(final ToolListener t) {
						t.onSyncClicked();
					}
				});
			}
		});
		//		final SearchView search = (SearchView)tool.findViewById(R.id.searchView1);
		//		search.setIconifiedByDefault(false);
		//		search.setOnQueryTextListener(new OnQueryTextListener() {
		//
		//			@Override
		//			public boolean onQueryTextSubmit(final String query) {
		//				editBroadcaster.broadcast(new Delegate<ToolListener>() {
		//					@Override
		//					public void invoke(final ToolListener t) {
		//						t.searchRequested(query);
		//					}
		//				});
		//				return true;
		//			}
		//
		//			@Override
		//			public boolean onQueryTextChange(final String newText) {
		//				return false;
		//			}
		//		});
		final ImageButton search = (ImageButton)tool.findViewById(R.id.search);

		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				editBroadcaster.broadcast(new Delegate<ToolListener>() {
					@Override
					public void invoke(final ToolListener t) {
						t.onSearchClicked();
					}
				});
			}
		});
	}
	public boolean getEditState() {
		return edit.isChecked();
	}

	public boolean registerToolListener(final ToolListener listener) {
		listener.onEditChecked(getEditState());
		return editBroadcaster.register(listener);
	}
	public boolean registerToolListener(final ToolListener listener,final boolean invokeNow) {
		if (invokeNow) listener.onEditChecked(getEditState());
		return editBroadcaster.register(listener);
	}
	public boolean unregisterToolListener(final ToolListener listener) {
		return editBroadcaster.unregister(listener);
	}
	public void setIcon(final int iconRes) {
		((ImageView)tool.findViewById(R.id.icon)).setImageResource(android.R.drawable.ic_menu_today);
	}
	public void setHeader(final int headerId) {
		header.setVisibility(View.VISIBLE);
		header.setText(headerId);
	}
	public void setSubHeader(final int subHeaderId) {
		subHeader.setVisibility(View.VISIBLE);
		subHeader.setText(subHeaderId);
	}
	public void setHeader(final String headerText) {
		header.setVisibility(View.VISIBLE);
		header.setText(headerText);
	}
	public void setSubHeader(final String subHeaderText) {
		if ( subHeaderText!=null ) {
			subHeader.setVisibility(View.VISIBLE);
			subHeader.setText(subHeaderText);
		} else {
			subHeader.setVisibility(View.INVISIBLE);
		}
	}


}
